import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:qaun_ni_dev/model/header_portrait_model.dart';

import 'package:qaun_ni_dev/pages/headPortrait/header_detail.dart';
import 'package:qaun_ni_dev/provider/header_portrait_provider.dart';
import 'package:qaun_ni_dev/utils/toast.dart';

class HeaderPortraitPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('头像审核'),
      ),
      body: HeaderPortraitList()
    );
  }
}

class HeaderPortraitList extends StatefulWidget {
  @override
  _HeaderPortraitListState createState() => _HeaderPortraitListState();
}

class _HeaderPortraitListState extends State<HeaderPortraitList> {

  GlobalKey<RefreshFooterState> _footerKeyPortrait = new GlobalKey<RefreshFooterState>();
  GlobalKey<RefreshHeaderState> _headerKeyPortrait = new GlobalKey<RefreshHeaderState>();


  @override
  void initState() {
    super.initState();
    _getList();

  }

  void _getList() {
    Provider.of<HeaderPortraitProvider>(context,listen: false).getHeaderPortraitList(context);
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Consumer<HeaderPortraitProvider>(
        builder: (context,data,_){
          if (data.headerPortraitData.length > 0) {
            return Container(
              padding: EdgeInsets.only(top: 10,left: 15,right: 15),
              child: EasyRefresh(
                  refreshFooter: ClassicsFooter(
                      key:_footerKeyPortrait,
                      bgColor:Colors.white,
                      textColor:Colors.pink,
                      moreInfoColor: Colors.pink,
                      showMore:true,
                      noMoreText: '没有更多了',
                      moreInfo:'加载中',
                      loadReadyText:'上拉加载'
                  ),
                  refreshHeader:ClassicsHeader(
                      key: _headerKeyPortrait,
                      refreshText: "下拉刷新数据",
                      refreshReadyText: "准备刷新数据",
                      refreshingText: "数据刷新中...",
                      refreshedText: "刷新完成",
                      bgColor: Colors.blue,
                      textColor: Colors.white,
                      moreInfoColor: Colors.white,
                      refreshHeight: 70.0,
                      isFloat: false,
                      showMore: false,
                      moreInfo: "Updated at %T"
                  ),
                  child: ListView.builder(
                      itemCount: data.headerPortraitData.length,
                      itemBuilder: (context,index) {
                        return _itemPortraitList(data.headerPortraitData,index);
                      }
                  ),
                  onRefresh: () async{
                    print('上拉刷新');
                    Provider.of<HeaderPortraitProvider>(context,listen: false).getHeaderPortraitList(context);
                  },
                loadMore: () {
                  Provider.of<HeaderPortraitProvider>(context,listen: false).getHeaderPortraitMoreList(context);
                },


              ),
            );

          } else {
            return Center(
              child: Text('暂无数据或者数据加载中'),
            );
          }
        },
      ),
    );
  }

  Widget _itemPortraitList(List portraitList,int index) {
    return InkWell(
      child: Container(
        margin: EdgeInsets.only(bottom: 10),
        decoration: BoxDecoration(
            border: Border.all(width: 1, color: Colors.black12),
            borderRadius: BorderRadius.all(Radius.circular(6))),
        child: Row(
          children: <Widget>[
            _leftOrderNumber(portraitList, index),
            _rightContent(portraitList, index),
          ],
        ),
      ),
      onTap: () {
        Navigator.push(context, MaterialPageRoute(
            builder: (context) {
              return HeaderDetail(portraitList[index]);
            }
        ));
      },
    );
  }

  Widget _leftOrderNumber(newGiftList, index) {
    return Container(
      margin: EdgeInsets.only(right: 10),
      padding: EdgeInsets.only(left: 6),
      width: 100,
      child: Column(
        children: <Widget>[
         Container(
           padding: EdgeInsets.only(top: 7,right: 18,left: 18,bottom: 7),
           margin: EdgeInsets.only(bottom: 20),
           decoration: BoxDecoration(
             borderRadius: BorderRadius.all(Radius.circular(6)),
             color: Colors.lightGreen,
           ),
           child: InkWell(
             child: Text('通过'),
             onTap: (){
               return _agreeHeader(context,newGiftList[index]);

             },
           ),
         ),
         Container(
           padding: EdgeInsets.only(top: 7,right: 18,left: 18,bottom: 7),
           decoration: BoxDecoration(
             borderRadius: BorderRadius.all(Radius.circular(6)),
             color: Colors.redAccent,
           ),
           child: InkWell(
             child: Text('拒绝'),
             onTap: (){
               print('拒绝');
               return _refuseHeader(context,newGiftList[index]);

             },
           ),
         ),
        ],
      ),
    );
  }

  Widget _rightContent(newGiftList, index) {
    return Container(
      width: 215,
      padding: EdgeInsets.only(left: 8, right: 6, top: 6, bottom: 6),
      decoration: BoxDecoration(
          border: Border(left: BorderSide(color: Colors.grey, width: 1),),
      ),
      child: Column(
            children: <Widget>[
              Container(
                width: 70,
                child: Text(
                  '用户新头像',
                ),
              ),
              Container(
                width: 100,
                child: Center(
                  child: Image.network(newGiftList[index].avatarUrl,fit: BoxFit.fill,),
                )
              ),
            ],
          ),
    );
  }

  Widget _agreeHeader(context, headerData) {
    showDialog<Null>(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text('提示'),
          content: Text('该头像确认通过吗？'),
          actions: <Widget>[
            FlatButton(
              child: Text("取消"),
              onPressed: () {
                Navigator.of(context).pop();
              } , //关闭对话框
            ),
            FlatButton(
              child: Text("通过"),
              onPressed: () {
                var formData = {
                  'isDel': 'N',
                  'uId': headerData.uid
                };
                Provider.of<HeaderPortraitProvider>(context,listen: false).isPassCheck(context, formData);
                Navigator.of(context).pop(); //关闭对话框
              },
            ),
          ],
        );
      },
    ).then((val) {
      print(val);
    });
  }

  Widget _refuseHeader(context, headerData) {
    showDialog<Null>(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text('提示'),
          content: Text('该头像确认拒绝吗？'),
          actions: <Widget>[
            FlatButton(
              child: Text("取消"),
              onPressed: () {
                Navigator.of(context).pop();
              } , //关闭对话框
            ),
            FlatButton(
              child: Text("拒绝"),
              onPressed: () {
                var formData = {
                  'isDel': 'Y',
                  'uId': headerData.uid
                };
                Provider.of<HeaderPortraitProvider>(context,listen: false).isPassCheck(context, formData);
                Toast.toast(context,msg: '操作成功');
                Navigator.of(context).pop(); //关闭对话框
              },
            ),
          ],
        );
      },
    ).then((val) {
      print(val);
    });
  }

}



